package com.cnswhy.cloud.server.app.controller.iot;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.app.dto.IotChargingLineLinkMerchantDTO;
import com.cnswhy.cloud.server.core.dto.IotChargingLineScaleSaveDTO;
import com.cnswhy.cloud.server.core.dto.IotChargingLineScaleUpdateDTO;
import com.cnswhy.cloud.server.core.entity.*;
import com.cnswhy.cloud.server.core.query.QueryGenerator;
import com.cnswhy.cloud.server.core.service.IotChargingLineInfoService;
import com.cnswhy.cloud.server.core.service.IotChargingLineScaleService;
import com.cnswhy.cloud.server.core.service.IotTerminalInfoService;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import com.cnswhy.cloud.server.core.vo.RespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
@Api(tags = "充电线")
@Controller
@RequestMapping("/iot/chargingline")
public class IotCLController extends BaseController {
    @Autowired
    IotChargingLineInfoService iotChargingLineInfoService;
    @Autowired
    IotTerminalInfoService iotTerminalInfoService;
    @Autowired
    IotChargingLineScaleService iotChargingLineScaleService;
    @Autowired
    MerchantService merchantService;

    @ApiOperation(value = "添加价格菜单", httpMethod = "POST")
    @RequestMapping(value = "/save", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo save(HttpServletRequest request, IotChargingLineScaleSaveDTO iotChargingLineScaleSaveDTO) {
        try{
            if(StringUtils.isBlank(iotChargingLineScaleSaveDTO.getTerminalId())){
                throw new BizException("终端编号不可为空");
            }
            if(iotChargingLineScaleSaveDTO.getHours() == null){
                throw new BizException("小时数不可为空");
            }
            if(iotChargingLineScaleSaveDTO.getHours() >= 1 && iotChargingLineScaleSaveDTO.getHours() <= 12){
            }else if(iotChargingLineScaleSaveDTO.getHours() == 24){
            }else{
                throw new BizException("小时数暂时支持1-12小时或者24小时");
            }
            if(iotChargingLineScaleSaveDTO.getMenuAmt() == null){
                throw new BizException("单价金额不可为空");
            }
            IotChargingLineInfo iotChargingLineInfo = iotChargingLineInfoService.getOne(Wraps.<IotChargingLineInfo>lbQ().eq(IotChargingLineInfo::getTerminalId,iotChargingLineScaleSaveDTO.getTerminalId()));
            IotChargingLineScale iotChargingLineScale = new IotChargingLineScale();

            iotChargingLineScale.setMenuAmt(iotChargingLineScaleSaveDTO.getMenuAmt());
            iotChargingLineScale.setHours(iotChargingLineScaleSaveDTO.getHours());
            iotChargingLineScale.setTerminalId(iotChargingLineInfo.getId());
            iotChargingLineScale.setMenuName(String.format("%d小时", iotChargingLineScaleSaveDTO.getHours()));
            iotChargingLineScaleService.save(iotChargingLineScale);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加设备失败");
        }
    }
    @ApiOperation(value = "修改价格", httpMethod = "POST")
    @RequestMapping(value = "/update", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo update(HttpServletRequest request, IotChargingLineScaleUpdateDTO iotChargingLineScaleUpdateDTO) {
        try{
            if(iotChargingLineScaleUpdateDTO.getId() == null){
                throw new BizException("终端ID不可为空");
            }
            if(iotChargingLineScaleUpdateDTO.getMenuAmt() == null){
                throw new BizException("单价金额不可为空");
            }
            IotChargingLineScale iotChargingLineScale = iotChargingLineScaleService.getById(iotChargingLineScaleUpdateDTO.getId());
            iotChargingLineScale.setMenuAmt(iotChargingLineScaleUpdateDTO.getMenuAmt());
            iotChargingLineScaleService.updateById(iotChargingLineScale);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("修改价格失败");
        }
    }
    @ApiOperation(value = "商户修改增益价格", httpMethod = "POST")
    @RequestMapping(value = "/merchantUpdate", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo merchantUpdate(HttpServletRequest request, IotChargingLineScaleUpdateDTO iotChargingLineScaleUpdateDTO) {
        try{
            if(iotChargingLineScaleUpdateDTO.getId() == null){
                throw new BizException("终端ID不可为空");
            }
            if(iotChargingLineScaleUpdateDTO.getMenuAmt() == null){
                throw new BizException("单价金额不可为空");
            }
            IotChargingLineScale iotChargingLineScale = iotChargingLineScaleService.getById(iotChargingLineScaleUpdateDTO.getId());
            iotChargingLineScale.setMerchantAmt(iotChargingLineScaleUpdateDTO.getMenuAmt());
            iotChargingLineScaleService.updateById(iotChargingLineScale);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("修改价格失败");
        }
    }
    @ApiOperation(value = "删除价格", httpMethod = "POST")
    @RequestMapping(value = "/remove", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo remove(HttpServletRequest request, Long id) {
        try{
            if(id == null){
                throw new BizException("终端ID不可为空");
            }
            iotChargingLineScaleService.removeById(id);

            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("删除价格失败");
        }
    }
    @ApiOperation(value = "查询设备收费列表", httpMethod = "POST")
    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo list(HttpServletRequest request, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String terminalId) {
        try{
            Customer customer = getLoginCustomer(request);
            if(StringUtils.isBlank(terminalId)){
                throw new BizException("终端编号不可为空");
            }
            IotChargingLineInfo iotChargingLineInfo = iotChargingLineInfoService.getOne(Wraps.<IotChargingLineInfo>lbQ().eq(IotChargingLineInfo::getTerminalId,terminalId));

            IotChargingLineScale iotChargingLineScale = new IotChargingLineScale();
            iotChargingLineScale.setTerminalId(iotChargingLineInfo.getId());
            QueryWrapper<IotChargingLineScale> queryWrapper = QueryGenerator.initQueryWrapper(iotChargingLineScale);
            queryWrapper.orderByDesc("create_time");
            Page<IotChargingLineScale> page = new Page<>(currentPage, pageSize);
            IPage<IotChargingLineScale> pageList = iotChargingLineScaleService.page(page, queryWrapper);
            return RespVo.success(pageList);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("查询设备收费列表失败");
        }
    }
}
